package com.jacobson.core.framework.configuration

import android.app.Application
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor

/**
 * ================================================
 * OkHttpClient配置类
 * @author Wucz on 2018/7/18 19:13
 * ================================================
 */
class OkHttpClientConfiguration internal constructor(builder: Builder) {
    internal var connectTimeout: Long = TIME_OUT
    internal var readTimeout: Long = TIME_OUT
    internal var globalHttpHandler: GlobalHttpHandler = defaultGlobalHttpHandler
    internal var interceptors: MutableList<Interceptor> = arrayListOf()
    internal var onBuildInterceptor: OnBuildInterceptor? = null
    internal var httpLoggerLevel: HttpLoggingInterceptor.Level

    init {
        builder.let {
            connectTimeout = it.connectTimeout
            readTimeout = it.readTimeout
            globalHttpHandler = it.globalHttpHandler
            httpLoggerLevel = it.httpLoggerLevel
            interceptors.addAll(it.interceptors)
        }
    }

    class Builder internal constructor(init: Builder.() -> Unit) {
        internal var connectTimeout: Long = TIME_OUT
        internal var readTimeout: Long = TIME_OUT
        internal var globalHttpHandler: GlobalHttpHandler = defaultGlobalHttpHandler
        internal var httpLoggerLevel = HttpLoggingInterceptor.Level.NONE
        internal var interceptors: MutableList<Interceptor> = arrayListOf()
        internal var onBuilderInterceptor: OnBuildInterceptor? = null

        init {
            init()
        }

        fun httpLoggerLevel(closure: () -> HttpLoggingInterceptor.Level): Builder {
            this.httpLoggerLevel = closure()
            return this
        }

        fun connectTimeout(closure: () -> Long): Builder {
            this.connectTimeout = closure()
            return this
        }

        fun readTimeout(closure: () -> Long): Builder {
            this.readTimeout = closure()
            return this
        }

        fun globalHttpHandler(closure: () -> GlobalHttpHandler): Builder {
            this.globalHttpHandler = closure()
            return this
        }

        fun addInterceptor(closure: () -> Interceptor): Builder {
            this.interceptors.add(closure())
            return this
        }

        fun onBuilderInterceptor(closure: () -> OnBuildInterceptor): Builder {
            this.onBuilderInterceptor = closure()
            return this
        }

        fun build(): OkHttpClientConfiguration {
            return OkHttpClientConfiguration(this)
        }
    }

    interface GlobalHttpHandler {
        fun onHttpRequestBefore(chain: Interceptor.Chain, request: Request): Request
        fun onHttpResultResponse(httpResult: String, chain: Interceptor.Chain, response: Response): Response
    }

    interface OnBuildInterceptor {
        fun onBuildIntercept(application: Application, builder: OkHttpClient.Builder)
    }

    companion object {
        internal const val TIME_OUT = 10000L
        fun build(init: Builder.() -> Unit): OkHttpClientConfiguration = Builder(init).build()

        internal val defaultGlobalHttpHandler = object : GlobalHttpHandler {
            override fun onHttpRequestBefore(chain: Interceptor.Chain, request: Request): Request {
                return request
            }

            override fun onHttpResultResponse(httpResult: String, chain: Interceptor.Chain, response: Response): Response {
                return response
            }

        }
    }
}